//
//  FillingRect.swift
//  CPT2
//
//  Created by onemade on 12/7/15.
//  Copyright © 2015 CNPC. All rights reserved.
//

import UIKit

@IBDesignable
class FillingRect: UIView {
    override func drawRect(rect: CGRect) {
//        //part one
//        UIColor.brownColor().setFill()
//        UIRectFill(rect)
//        
//        //part two
//        UIColor.whiteColor().setStroke()
//        let frame = CGRectMake(20, 30, 100, 300)
//        UIRectFrame(frame)
        
        //part three
//        let title: NSString = "底层图片渲染"
//        let font = UIFont.systemFontOfSize(34)
//        let attr = [NSFontAttributeName: font]
//        title.drawAtPoint(CGPoint(x: 100, y: 20), withAttributes: attr)
////        let paper = UIImage(named: "paper1")
//        let imagePath = NSBundle.mainBundle().pathForResource("paper", ofType: "png")
//        let paper = UIImage(contentsOfFile: imagePath!)
//        paper?.drawInRect(CGRectMake(0, 65, min(self.frame.size.width, self.frame.size.height), 400))
        
        //QUARTZ
        //part four
//        let context = UIGraphicsGetCurrentContext()
//
//        CGContextMoveToPoint(context, 75, 10)
//        CGContextAddLineToPoint(context, 10, 150)
//        CGContextAddLineToPoint(context, 160, 150)
//        CGContextClosePath(context)
//        
//        CGContextSetLineWidth(context, 10)
//        UIColor.redColor().setStroke()
//        UIColor.blueColor().setFill()
//        CGContextSaveGState(context)
//        CGContextSetLineWidth(context, 5)
//        UIColor.greenColor().setFill()
//        CGContextRestoreGState(context)
//        CGContextDrawPath(context, .FillStroke)
        
        //part five quartz bezier path
//        let context = UIGraphicsGetCurrentContext()
//        CGContextMoveToPoint(context, 333, 0)
//        CGContextAddCurveToPoint(context, 333, 0, 332, 26, 330, 26)
//        CGContextAddCurveToPoint(context, 330, 26, 299, 20, 299, 17)
//        CGContextAddLineToPoint(context, 296, 17)
//        CGContextAddCurveToPoint(context, 296, 17, 296, 19, 291, 19)
//        CGContextAddLineToPoint(context, 250, 19)
//        CGContextAddCurveToPoint(context, 250, 19, 241, 24, 238, 19)
//        CGContextAddCurveToPoint(context, 236, 20, 234, 24, 227, 24)
//        CGContextAddCurveToPoint(context, 220, 24, 217, 19, 216, 19)
//        CGContextAddCurveToPoint(context, 214, 20, 211, 22, 207, 20)
//        CGContextAddCurveToPoint(context, 207, 20, 187, 20, 182, 21)
//        CGContextAddLineToPoint(context, 100, 45)
//        CGContextAddLineToPoint(context, 97, 46)
//        CGContextAddCurveToPoint(context,97, 46, 86, 71, 64, 72)
//        CGContextAddCurveToPoint(context, 42, 74, 26, 56, 23, 48)
//        CGContextAddLineToPoint(context, 9, 47)
//        CGContextAddCurveToPoint(context, 9, 47, 0, 31, 0, 0)
//        CGContextStrokePath(context)
        
        //part six quartz draw image
////        let imagePath = NSBundle.mainBundle().pathForResource("logo", ofType: "png")
////        let img = UIImage(contentsOfFile: imagePath!)
//        let img = UIImage(named: "logo")
//        let image = img!.CGImage
//        let context = UIGraphicsGetCurrentContext()
//        CGContextSaveGState(context)
//        //for uikit coordinate
//        CGContextTranslateCTM(context, 0, img!.size.height)
//        CGContextScaleCTM(context, 1, -1)
//        let touchRect = CGRectMake(44, -44, img!.size.width, img!.size.height)
//        CGContextDrawImage(context, touchRect, image)
        
        //part seven quartz CTM
//        let imagePath = NSBundle.mainBundle().pathForResource("logo", ofType: "png")
//        let img = UIImage(contentsOfFile: imagePath!)
//        let image = img!.CGImage
//        let context = UIGraphicsGetCurrentContext()
//        CGContextSaveGState(context)
//        CGContextTranslateCTM(context, 150, 50)
//        CGContextScaleCTM(context, 0.5, 0.75)
//        CGContextRotateCTM(context, CGFloat(M_PI_4 ))
//        let touchRect = CGRectMake(0, 44, img!.size.width, img!.size.height)
//        CGContextDrawImage(context, touchRect, image)
//        CGContextRestoreGState(context)
        
        //part eight affine
        let imagePath = NSBundle.mainBundle().pathForResource("logo", ofType: "png")
        let img = UIImage(contentsOfFile: imagePath!)
        let image = img!.CGImage
        let context = UIGraphicsGetCurrentContext()
        CGContextSaveGState(context)
        var myAffine = CGAffineTransformTranslate(self.transform, 0, img!.size.height)
        myAffine = CGAffineTransformScale(myAffine, 1, -1)
        CGContextConcatCTM(context, myAffine)
        let touchRect = CGRectMake(0, 0, img!.size.width, img!.size.height)
        CGContextDrawImage(context, touchRect, image)
        CGContextRestoreGState(context)
        
        
    }
}
